<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="script/vue-2.4.0.js"></script>
</head>

<body>
    <div id="app">
        <table>
            <tr>
                <td>{{good.name}}</td>
                <td>{{good.price}}</td>
                <td>
                    <button @click="sub">-</button> {{good.quantity}}
                    <button @click="add">+</button>
                </td>

                <td>{{totalPrice}}</td>
                <td>{{totalPriceArgs(good.price,good.quantity)}}</td>
            </tr>
        </table>
    </div>
    <script>
        Vue.config.productionTip = false //关闭生成信息提示
        var vm = new Vue({
            el: '#app',
            data: {
                good: {
                    name: '三星手机',
                    price: 399,
                    quantity: 1,
                }
            },
            methods: {
                sub() {
                    this.good.quantity--
                },
                add() {
                    this.good.quantity++
                }
            },
            computed: {
                totalPrice() {
                    console.log('重新计算')
                    return this.good.price * this.good.quantity
                },
                //传递参数，进行小计计算
                totalPriceArgs() {
                    return function(p, q) { //计算属性传递参数，参数1p代表价格 参数2传递q,代表购买数量
                        return p * q

                    }
                }
            }
        })
    </script>
</body>

</html>